package com.wshl.aijiuunion.controller.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.GsonUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wshl.aijiuunion.BaseActivity;
import com.wshl.aijiuunion.R;
import com.wshl.aijiuunion.callback.RequestCallback;
import com.wshl.aijiuunion.model.RequestModel;
import com.wshl.aijiuunion.utils.BarUtils;
import com.wshl.aijiuunion.view.adapter.VideoRecordAdapter;
import com.wshl.aijiuunion.view.bean.VideoRecordBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class VideoListActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.recycle_videolist)
    RecyclerView recycleVideo;
    @BindView(R.id.refresh_videolist)
    SmartRefreshLayout refreshVideolist;
    @BindView(R.id.tv_nocontent)
    TextView tvNocontent;


    private RequestModel model;
    private int page = 1;
    private VideoRecordAdapter adapter;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_videolist;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        BarUtils.setWhiteStatusBarForActivity(this);
        tvTopTitle.setText("观看记录");
        model = new RequestModel();
        recycleVideo.setLayoutManager(new LinearLayoutManager(this));

        getVideoList(false);
        listdata = new ArrayList<>();

    }

    private List<VideoRecordBean.DataBeanX.DataBean> listdata;

    private void getVideoList(boolean isLoadmore) {
        model.videoRecord(this, page, new RequestCallback() {
            @Override
            public void onSuccess(String response) {

                VideoRecordBean videoRecordBean = GsonUtils.fromJson(response, VideoRecordBean.class);
                VideoRecordBean.DataBeanX data = videoRecordBean.getData();
                List<VideoRecordBean.DataBeanX.DataBean> data1 = data.getData();
                if (isLoadmore) {
                    refreshVideolist.finishLoadMore();
                } else {
                    refreshVideolist.finishRefresh();
                }
                if (adapter == null) {
                    listdata = new ArrayList<>();
                    listdata.addAll(data1);
                    if (listdata.size() == 0) {
                    tvNocontent.setVisibility(View.VISIBLE);
                    recycleVideo.setVisibility(View.GONE);
                        return;
                    }
                    tvNocontent.setVisibility(View.GONE);
                    recycleVideo.setVisibility(View.VISIBLE);
                    adapter = new VideoRecordAdapter(recycleVideo);
                    adapter.setData(data1);
                    recycleVideo.setAdapter(adapter);
                } else if (isLoadmore) {

                    listdata.addAll(data1);
                    adapter.notifyDataSetChanged();

                } else {
                    listdata.clear();
                    listdata.addAll(data1);
                    if (listdata.size() == 0) {
                        tvNocontent.setVisibility(View.VISIBLE);
                        recycleVideo.setVisibility(View.GONE);
                    }else{
                        tvNocontent.setVisibility(View.GONE);
                        recycleVideo.setVisibility(View.VISIBLE);
                    }

                    adapter.notifyDataSetChanged();
                }


            }

            @Override
            public void onError(int code) {
                if (isLoadmore) {
                    refreshVideolist.finishLoadMore();
                } else {
                    refreshVideolist.finishRefresh();
                }
            }

            @Override
            public void onIntentError() {

            }
        });
    }


    @OnClick(R.id.img_back)
    public void onViewClicked() {
        this.finish();
    }


}
